package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
<<<<<<< Updated upstream
import com.tanhua.dubbo.server.pojo.Log;
=======
>>>>>>> Stashed changes
import com.tanhua.dubbo.server.pojo.UserFreeze;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(version = "1.0.0")
<<<<<<< Updated upstream
public class UserFreezeApiImpl implements UserFreezeApi {
=======
public class UserFreezeApiImpl implements UserFreezeApi{
>>>>>>> Stashed changes

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean freezeUser(UserFreeze userFreeze) {
        Query query = Query.query(Criteria.where("userId").is(userFreeze.getUserId()));
        try {
            UserFreeze one = mongoTemplate.findOne(query, UserFreeze.class);

            //把天数，转变成系统时间存储
            Long day = userFreeze.getFreezingTime();
<<<<<<< Updated upstream
            long freezing = 0;
            if (day == 1) {
                freezing = System.currentTimeMillis() + 3 * 24 * 60 * 60 * 1000;
            }
            if (day == 2) {
                freezing = System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000;
            }
            if (day == 3) {
                freezing = System.currentTimeMillis() << 2;
            }

            if (null == one) {
=======
            long freezing = System.currentTimeMillis() + day * 24 * 60 * 60 * 100;

            if (null==one){
>>>>>>> Stashed changes
                userFreeze.setId(ObjectId.get());
                userFreeze.setFreezingTime(freezing);
                mongoTemplate.save(userFreeze);
                return true;
            }

            //更新冻结原因
            Update update = new Update();

<<<<<<< Updated upstream
            update.set("freezingTime", freezing);
            update.set("freezingRange", userFreeze.getFreezingRange());
            update.set("reasonsForFreezing", userFreeze.getReasonsForFreezing());
            update.set("frozenRemarks", userFreeze.getFrozenRemarks());

            mongoTemplate.updateFirst(query, update, UserFreeze.class);
=======
            update.set("freezingTime",freezing);
            update.set("freezingRange",userFreeze.getFreezingRange());
            update.set("reasonsForFreezing",userFreeze.getReasonsForFreezing());
            update.set("frozenRemarks",userFreeze.getFrozenRemarks());
            mongoTemplate.updateFirst(query,update,UserFreeze.class);
>>>>>>> Stashed changes
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public Boolean unfreezeUser(Integer userId, String reasonsForThawing) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserFreeze one = mongoTemplate.findOne(query, UserFreeze.class);
<<<<<<< Updated upstream
        if (null == one) {
=======
        if (null==one){
>>>>>>> Stashed changes
            return true;
        }

        //更新解冻原因
        Update update = new Update();

<<<<<<< Updated upstream
        update.set("freezingTime", System.currentTimeMillis());
        update.set("freezingRange", 0);
        update.set("reasonsForThawing", reasonsForThawing);
        mongoTemplate.updateFirst(query, update, UserFreeze.class);
=======
        update.set("freezingTime",System.currentTimeMillis());
        update.set("freezingRange",0);
        update.set("reasonsForThawing",reasonsForThawing);
        mongoTemplate.updateFirst(query,update,UserFreeze.class);
>>>>>>> Stashed changes
        return true;
    }

    /**
<<<<<<< Updated upstream
=======
     *
>>>>>>> Stashed changes
     * @param userId
     * @return ture 冻结
     */
    @Override
    public Boolean findFreezeStatue(Integer userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserFreeze one = mongoTemplate.findOne(query, UserFreeze.class);
<<<<<<< Updated upstream
        if (null == one) {
=======
        if (null==one){
>>>>>>> Stashed changes
            return false;
        }
        return one.getFreezingTime() >= System.currentTimeMillis();
    }

    @Override
    public Map<Integer, String> findFreezeStatueByIds(List ids) {
        Query query = Query.query(Criteria.where("userId").in(ids));
        List<UserFreeze> freezeList = mongoTemplate.find(query, UserFreeze.class);
        HashMap<Integer, String> map = new HashMap<>();

        for (UserFreeze userFreeze : freezeList) {
            //冻结 2
<<<<<<< Updated upstream
            if (userFreeze.getFreezingTime() >= System.currentTimeMillis()) {
                map.put(userFreeze.getUserId(), "2");
            } else {
                map.put(userFreeze.getUserId(), "1");
=======
            if (userFreeze.getFreezingTime() >= System.currentTimeMillis()){
                map.put(userFreeze.getUserId(),"2");
            }else {
                map.put(userFreeze.getUserId(),"1");
>>>>>>> Stashed changes
            }
        }
        return map;
    }
<<<<<<< Updated upstream

    @Override
    public Map<Integer, Long> findUserLastTimeByIds(List<Long> ids) {
        Query query = Query.query(Criteria.where("userId").in(ids));
        List<Log> logs = mongoTemplate.find(query, Log.class);

        HashMap<Integer, Long> lastTime = new HashMap<>();

        for (Log log : logs) {
            Integer userId = log.getUserId();
            Long time = lastTime.get(userId);
            if (time == null) {
                lastTime.put(userId, log.getLogTime());
            }
            if (time != null) {
                lastTime.put(userId, time > log.getLogTime() ? time : log.getLogTime());
            }
        }

        return lastTime;
    }
=======
>>>>>>> Stashed changes
}
